<?php namespace Common\Model;

use \Hdphp\Model\Model;

class Orders extends Model{
	
	protected $table = 'orders';
	
	public function addData(){
		//接收收货地址的said
		$said = Q('post.said',0,'intval');
		//接收提交过来的支付方式
		$paytype = Q('post.paytype');
		//接收提交过来的送货时间
		$paytime = Q('post.paytime');
		//接收提交过来的送货前是否联系的值
		$callme = Q('post.callme');
		//接收提交过来的备注
		$remark = Q('post.remark');
		//获得订单总价
		$totalprice = Cart::getTotalPrice();
		$shippingaddModel = new \Common\Model\Shippingaddress;
		//获得收货地址的省份
		$province = $shippingaddModel->where("said={$said}")->pluck('province');
		//获得收货的地址的城市
		$city = $shippingaddModel->where("said={$said}")->pluck('city');
		//获得收货地址的区或县
		$county = $shippingaddModel->where("said={$said}")->pluck('county');
		//获得详细地址
		$address = $shippingaddModel->where("said={$said}")->pluck('address');
		//组合完整的收货地址
		$shippingaddress = $province.$city.$county.$address;
		//取得收货人姓名
		$consignee = $shippingaddModel->where("said={$said}")->pluck('name');
		//取得收货人联系电话
		$phone = $shippingaddModel->where("said={$said}")->pluck('phone');
		//生成一个订单号码
		$ordernumber = Cart::getOrderId();
		//获得登陆的时候保存的用户ID
		$user_cusid = $_SESSION['cusid'];
		//组合数组 添加进数据库
		$orderdata = array(
			'generatedtime'=>time(),
			'remark'=>$remark,
			'totalprice'=>$totalprice,
			'shippingaddress'=>$shippingaddress,
			'consignee'=>$consignee,
			'phone'=>$phone,
			'ordernumber'=>$ordernumber,
			'user_cusid'=>$user_cusid,
			'paytype'=>$paytype,
			'deliverytime'=>$paytime,
			'callme'=>$callme
		);
		//调用add方法 添加数据进order表 并接收返回的$oid
		$oid = $this->add($orderdata);
		$orderlistModel = new \Common\Model\Orderlist;
		//获得购物订单数组
		$goodsData = cart::getGoods();
		//遍历购物订单数组  组合新数组 调用add方法把新数组添加进数据库
		foreach ($goodsData as $k => $v) {
			$orderlistdata = array(
				'quantity'=>$v['num'],
				'price'=>$v['total'],
				'orders_oid'=>$oid,
				'goods_gid'=>$v['id'],
				'listpic'=>$v['listpic'],
				'gname'=>$v['name'],
			);
			//声明空字符串 用于组合商品规格字符串 
			$str = '';
			foreach ($v['options'] as $key => $value) {
				$str .= $key."：".$value." ";
			}
			//把组合好的字符串压进新数组
			$orderlistdata['spec'] = $str;
			//调用add方法 把数据添加进数据库
			$orderlistModel->add($orderlistdata);
		}
		//返回添加数据进order表后的oid
		return $oid;	
	}
	
} 



 ?>